<template>
  <van-popup v-model="popupShow" position="right" :style="{ height: '100%', width: '100%' }">
    <div class="doc-index">
      <top-header :left-click="headerBack">
        <template #title>项目文档</template>
      </top-header>
      <div>
        <div class="bread-crumb">
          <div class="left">
            <div>
              <span @click="topCatalog" :class="{ now: navQueue.length === 0 }">主目录</span>
            </div>
            <div v-show="navQueue.length > 1">
              <span class="arrow">&gt;</span>
              <span @click="prevCatalog">上一级</span>
            </div>
            <div v-show="navQueue.length > 0">
              <span class="arrow">&gt;</span>
              <span class="now" :class="[`catalogName-${navQueue.length > 1 ? 2 : 1}`]">{{ catalogName }}</span>
            </div>
          </div>
          <div class="right" @click="showEmpty = !showEmpty">
            <span>空目录</span>
            <span class="iconfont iconcheck" :class="{ 'show-empty': showEmpty }"></span>
          </div>
        </div>
        <div class="common-list-body">
          <template v-for="item in list.catalogs">
            <div class="doc-common-block doc-catalog" v-show="(showEmpty && !item.counts) || item.counts" :key="item.guid" @click="nextCatalog(item)">
              <span class="iconfont iconwenjian"></span>
              <div class="title-div">
                <div class="text-1">{{ item.label }}</div>
                <div class="text-2">{{ item.counts ? item.counts : 0 }}个</div>
              </div>
              <span class="iconfont iconshouqi"></span>
              <van-divider></van-divider>
            </div>
          </template>

          <a :href="$filePreview(item.fileName, item.storagePath)" class="doc-common-block doc-file" v-for="item in list.attachments" :key="item.guid">
            <span :class="['iconfont', $tools.getFileIcon(item.storagePath)]"></span>
            <div class="title-div">
              <div class="text-1">{{ item.fileName }}</div>
              <div class="text-2">{{ $tools.fileSize(item.contentLength) }}</div>
            </div>
            <van-divider></van-divider>
          </a>

          <empty-one v-show="showEmptyOne"></empty-one>
        </div>
      </div>
    </div>
  </van-popup>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import Base from "@/utils/base";
import TopHeader from "@/components/TopHeader.vue";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import { Divider, Popup } from "vant";
import EmptyOne from "@/components/EmptyOne.vue";

@Component({
  name: "DocComponentIndex",
  components: {
    TopHeader,
    SearchOne,
    EmptyOne,
    VanDivider: Divider,
    VanPopup: Popup
  }
})
export default class DocComponentIndex extends Base {
  private level = 0;
  // 项目id
  private projectId = "";
  // 类目名称
  private catalogName = "";
  // 列表数据
  private list: any = {
    catalogs: [],
    attachments: []
  };
  // 显示空文案
  private showEmpty = true;

  private popupShow = false;

  // 面包屑的导航队列
  private navQueue: any[] = [];

  private created() {
    this.projectId = (this.$route.query.projectId ? this.$route.query.projectId : "") as string;
  }

  /**
   * 跟新目录导航队列
   * @param {object|undefined} 如果是undefined代表最后一项移除队列
   */
  private updateQueue(item?: any) {
    if (item) {
      this.navQueue.push({ label: item.label, guid: item.guid });
    } else {
      this.navQueue.pop();
    }
  }

  /**
   * 初始化参数
   */
  private init() {
    this.level = 0;
    this.navQueue = [];
    this.catalogName = "";
  }

  /**
   * 显示popup
   * 父组件通过$refs.xxx.show()调用子组件的方式
   */
  public async show() {
    await this.getData();
    this.init();
    this.popupShow = true;
  }

  private async getData(item?: any) {
    const toast = this.$toast.loading({
      message: "正在加载...",
      forbidClick: true,
      duration: 0
    });

    const res = await Api.FileQueryTree({
      level: item ? this.level : 0,
      projectId: this.projectId,
      catalogId: item && item.guid ? item.guid : ""
    });

    toast.clear();

    if (res && res.code === 200) {
      this.list = res.data;
      return true;
    }
    return false;
  }

  /**
   * 下一级目录
   * 接口请求成功后，才会执行队列的更新
   */
  private async nextCatalog(item: any) {
    this.level++;
    const res: boolean = await this.getData(item);
    if (res) {
      this.updateQueue(item);
      this.catalogName = item.label;
    } else {
      this.level--;
    }
  }

  /**
   * 下一级目录
   * 先拿出队列中最后第一个元素，当入参，请求接口，如果请求成功，最后一个元素移除队列
   */
  private async prevCatalog() {
    this.level--;
    const prev = this.navQueue[this.navQueue.length - 2];
    const res: boolean = await this.getData({
      guid: prev.guid
    });

    if (res) {
      this.catalogName = prev.label;
      this.updateQueue();
    } else {
      this.level++;
    }
  }

  /**
   * 回到顶级目录
   * 清空队列，子目录名称
   */
  private topCatalog() {
    this.init();
    this.getData();
  }

  private headerBack() {
    this.popupShow = false;
  }

  // 是否显示空目录的提示文字
  private get showEmptyOne() {
    let _return = false;
    const catalogs = this.list.catalogs.some((item: any) => {
      return item.counts > 0;
    });

    if (
      ((!this.list.catalogs || this.list.catalogs.length === 0) && (!this.list.attachments || this.list.attachments.length === 0)) ||
      (!catalogs && (!this.list.attachments || this.list.attachments.length === 0) && !this.showEmpty)
    ) {
      _return = true;
    }
    return _return;
  }
}
</script>

<style lang="less" scoped>
.doc-index {
  background-color: #eff2f8;
  .common-list-body {
    height: calc(100vh - 44px - 47px - 12px);
    margin-top: 12px;
    background-color: white;
  }
  .bread-crumb {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #828795;

    height: 47px;

    background-color: white;
    .arrow {
      color: #c0c0c0;
    }
    .left {
      padding-left: 15px;
      font-size: 16px;
      display: flex;
      align-items: center;
      height: 100%;
      & > div {
        display: flex;
        align-items: center;
        height: 100%;
      }
      span {
        display: inline-block;
        color: #828795;
        line-height: 47px;
        // height: 47px;
      }

      .now {
        color: #5e9efb;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        word-break: break-all;
      }

      .catalogName-1 {
        width: 200px;
      }
      .catalogName-2 {
        width: 150px;
      }
    }
    .right {
      padding-right: 15px;
      display: flex;
      align-items: center;
      height: 100%;
      .iconcheck {
        width: 12px;
        height: 12px;
        border: 1px solid #979797;
        font-size: 11px;
        color: #979797;
        border-radius: 12px;
        text-align: center;
        line-height: 12px;
        margin-left: 4px;
      }
      .show-empty {
        background-color: #5e9efb;
        border-color: #5e9efb;
        color: white;
      }
    }
  }

  .doc-common-block {
    // border-bottom: 1px solid #e4e4e4;
    word-break: break-all;
    display: flex;
    align-items: center;
    line-height: initial;
    padding: 18px 20px;
    position: relative;
    &:last-child {
      border-bottom: 0px;
    }
    .iconfont {
      font-size: 31px;
      margin-right: 16px;
    }
    .van-divider {
      margin: 0;
      border-color: #e4e4e4;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
    }
    .iconshouqi {
      margin-right: 0;
      font-size: 18px;
      margin-left: 20px;
      transform: rotate(-90deg);
      color: #bdbdbd;
    }
    .title-div {
      width: 250px;
    }
  }
  // font-size: 14px;
}
</style>
